package com.jokerhung.mymoney;


import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;


public class translist extends ListActivity {
	private SQLiteDatabase db=null;
	private Cursor transCursor=null;
	
	public static final int EDIT_ID = Menu.FIRST+1;
	public static final int NEW_ID = Menu.FIRST+2;
	public static final int NEWFRMEM_ID = Menu.FIRST+3;
	public static final int DELETE_ID = Menu.FIRST+4;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	            
	    db = (new DatabaseHelper(this)).getWritableDatabase();
	    
	    transCursor = db.rawQuery("SELECT * FROM transactions", null);
	    
	    ListAdapter adapter=new SimpleCursorAdapter(this,
				R.layout.translistrow, transCursor,
				new String[] {"date", "fromfor", "amount", "category_id"},
				new int[] {R.id.translistrow_txtDate, R.id.translistrow_txtFromfor, R.id.translistrow_txtAmount, R.id.translistrow_txtCategory});
	    setListAdapter(adapter);
	    
	    registerForContextMenu(getListView());
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		transCursor.close();
		db.close();
	}
	
	public void onListItemClick(ListView parent, View v,
										int position, long id) {
		
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
						ContextMenu.ContextMenuInfo menuInfo) {
		populateMenu(menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		return(applyMenuChoice(item) ||
				super.onOptionsItemSelected(item));
	}
	
	private void populateMenu(Menu menu) {
		menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "Edit");
		menu.add(Menu.NONE, NEW_ID, Menu.NONE, "New ...");
		menu.add(Menu.NONE, NEWFRMEM_ID, Menu.NONE, "New From Memorized");
		menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete");
	}
	
	private boolean applyMenuChoice(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info;
		
		switch (item.getItemId()) {
			case EDIT_ID:
				info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
				edit(info.id);
				return(true);
			case DELETE_ID:
				info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
				delete(info.id);
				return(true);
			case NEW_ID:
				info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
				newtrans();
				return(true);
			case NEWFRMEM_ID:
				info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
				newtransfrmem();
				return(true);
		}

		return(false);
	}
	
	private void edit(final long rowId) {
		if (rowId>0) {
			Variables.current_transid = rowId;
			Intent intent = new Intent("com.jokerhung.mymoney.EDIT_TRANS_ITEM");
			startActivity(intent);
		}
	}
	
	private void delete(final long rowId) {
		String[] args={String.valueOf(rowId)};
		
		if (rowId>0) {
			db.delete("transactions", "_ID=?", args);
			transCursor.requery();
		}
		
		Toast
			.makeText(this, "Deleted", Toast.LENGTH_SHORT)
			.show();
	}
	
	private void newtrans() {
		
	}
	
	private void newtransfrmem() {
		
	}

}

